package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserRegParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*业务层实现类需要添加@Service注解,表示当前类为业务层实现类*/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务:userLoginParam={}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }


    @Override
    public void reg(UserRegParam userRegParam) {
        log.debug("用户注册业务:userLoginParam={}", userRegParam);
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        //如果用户id为null,则表示新增用户
        if (user.getId() == null) {
            user.setCreateTime(new Date());
            user.setPassword("123456");
            userMapper.insert(user);
        } else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> select(UserQuery userQuery) {
        log.debug("用户查询业务:userQuery={}", userQuery);
        List<UserVO> userVOList = userMapper.selectAll(userQuery);
        return userVOList;
    }

    @Override
    public void resetPassword(Long id) {
        log.debug("重置密码业务:id={}", id);
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("更新用户状态业务:id={},status={}", id, status);
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUserById(Long id) {
        log.debug("删除用户业务:id={}", id);
        userMapper.deleteUserById(id);
    }

    @Override
    public List<UserVO> selectAudit(Long parentId) {
        log.debug("查询审批人列表业务:parentId={}", parentId);
        ArrayList<UserVO> userVOS = new ArrayList<>();
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOS.add(auditUser1);
        if (auditUser1.getParentId() != null && auditUser1 != null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOS.add(auditUser2);
        }
        return userVOS;
    }

}

